package com.black.cat.jvm.classfile.constantpool.impl;


import com.black.cat.jvm.classfile.ClassReader;
import com.black.cat.jvm.classfile.constantpool.BaseConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantPool;
import com.black.cat.jvm.common.CommonValue;

/**
 * @Author blackcat
 * @create 2021/8/11 21:40
 * @version: 1.0
 * @description:一个动态方法调用点
 *
 * CONSTANT_InvokeDynamic_info {
 * u1 tag;
 * u2 bootstrap_method_attr_index;
 * u2 name_and_type_index;
 * }
 */
public class ConstantInfoInvokeDynamic extends BaseConstantInfo implements ConstantInfo {
    private ConstantPool constantPool;
    private int bootstrapMethodAttrIndex; // 对当前class文件中引导方法表的bootstrapMethod[]数组的索引
    private int nameAndTypeIndex;// 部分符号索引

    public ConstantInfoInvokeDynamic(ConstantPool constantPool, ClassReader reader) {
        this.constantPool = constantPool;
        this.bootstrapMethodAttrIndex = reader.nextU2ToInt();
        this.nameAndTypeIndex = reader.nextU2ToInt();
    }

    @Override
    public String getValue() {
        value = constantPool.getUtfStringByIndex(bootstrapMethodAttrIndex) + "-" + constantPool.getUtfStringByIndex(nameAndTypeIndex);
//        value = constantPool.getUtfStringByIndex(nameAndTypeIndex);
        return super.getValue();
    }

    @Override
    public int getTag() {
        return CommonValue.CONSTANT_POOL_TAG_INVOKE_DYNAMIC;
    }
}
